
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备价格" prop="equipmentPrice">
        <el-input
          v-model="queryParams.equipmentPrice"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="equipmentTypeid">
        <el-select v-model="queryParams.equipmentTypeid" placeholder="请选择设备类型"
        >
          <el-option
            v-for="item in equipemntType"
            :key="item.equipmentTypeid"
            :label="item.equipmentTypename"
            :value="item.equipmentTypeid"
            clearable
            @keyup.enter.native="handleQuery"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="设备数量" prop="equipmentNums">
        <el-input
          v-model="queryParams.equipmentNums"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hospital:equipemnt:add']"
        >新增设备</el-button>
      </el-col>

     <el-col :span="1.5">
       <el-button
         type="success"
         plain
         icon="el-icon-edit"
         size="mini"
         :disabled="single"
         @click="handleUpdate"
         v-hasPermi="['hospital:equipemnt:edit']"
       >修改</el-button>
     </el-col>

     <el-col :span="1.5">
       <el-button
         type="danger"
         plain
         icon="el-icon-delete"
         size="mini"
         :disabled="multiple"
         @click="handleDelete"
         v-hasPermi="['hospital:equipemnt:remove']"
       >删除</el-button>
     </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hospital:equipemnt:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipemntList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备ID" align="center" prop="equipmentId" />
      <el-table-column label="设备名称" align="center" prop="equipmentName" />
      <el-table-column label="设备数量" align="center" prop="equipmentNum" />
      <el-table-column label="价格" align="center" prop="equipmentPrice" />
      <el-table-column label="设备类型" align="center" prop="equipmentTypeName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateYanRu(scope.row)"
            v-hasPermi="['hospital:equipemnt:edit']"
          >入库</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateYanChu(scope.row)"
            v-hasPermi="['hospital:equipemnt:edit']"
          >出库</el-button>

<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['hospital:equipemnt:remove']"-->
<!--          >删除</el-button>-->

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--修改设备库存表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input :value="form.equipmentName" placeholder="请录入信息" />
        </el-form-item>
        <el-form-item label="设备价格" prop="equipmentPrice">
          <el-input :value="form.equipmentPrice" placeholder="请录入信息" />
        </el-form-item>
        <el-form-item label="设备数量" prop="equipmentNum">
          <el-input :value="form.equipmentNum" placeholder="请录入信息" />
        </el-form-item>
        <el-form-item label="设备类型" prop="equipmentNum">
          <el-input :value="form.equipmentTypeName" placeholder="请录入信息" />
        </el-form-item>
        <el-form-item label="设备出入库数量" prop="equipmentNum">
          <el-input v-model="form.equipmentNums" placeholder="请录入信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!--添加设备库存表对话框 -->
    <el-dialog :title="title" :visible.sync="openAdd" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请录入信息" />
        </el-form-item>
        <el-form-item label="设备价格" prop="equipmentPrice">
          <el-input v-model="form.equipmentPrice" placeholder="请录入信息" />
        </el-form-item>
        <el-form-item label="设备数量" prop="equipmentNum">
          <el-input v-model="form.equipmentNum" placeholder="请录入信息" />
        </el-form-item>

        <el-form-item label="设备类型" prop="equipmentTypeid">
          <el-select
            v-model="equipmentTypeIdYan"
            placeholder="请选择设备类型"
          >
            <el-option
              v-for="item in equipemntType"
              :key="item.equipmentTypeid"
              :label="item.equipmentTypename"
              :value="item.equipmentTypeid"
              @change="equipmentTypeChange(equipmentTypeIdYan)"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getEquipemntTypeYan,
  updateEquipemntYan,
  getEquipemntByIdYan,
  listEquipemntYan,
  listEquipemnt,
  getEquipemnt,
  delEquipemnt,
  addEquipemnt,
  updateEquipemnt,
  addEquipemntYan
} from '@/api/hospital/equipemnt'
import {listEquipment_type} from "@/api/hospital/equipment_type";

export default {
  name: "Equipemnt",
  data() {
    return {
      equipmentStatus:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      equipmentTypeIdYan:null,
      equipemntType:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备库存表表格数据
      equipemntList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openAdd: false,
      equipment_typeList:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentName: null,
        equipmentNum: null,
        equipmentNums: null,
        equipmentPrice: null,
        equipmentTypeid: null,
        equipmentTypeName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getListYan();
    this.getEquipemntTypeListYan();
  },
  methods: {
    /** 查询设备库存表列表 */
    getList() {
      this.loading = true;
      listEquipemnt(this.queryParams).then(response => {
        this.equipemntList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getListYan() {

      this.loading = true;
      listEquipemntYan(this.queryParams).then(response => {
        this.equipemntList = response.rows;
        console.log(this.equipemntList);
        this.total = response.total;
        this.loading = false;
      });
    },

    equipmentTypeChange(equipmentTypeIdYan) {
      this.form.equipmentTypeid = equipmentTypeIdYan
    },
    // 取消按钮
    cancel() {
      this.title = "",
      this.open = false;
      this.openAdd = false;
      this.equipmentTypeIdYan = null;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        equipmentId: null,
        equipmentName: null,
        equipmentNum: null,
        equipmentNums: null,
        equipmentPrice: null,
        equipmentTypeid: null,
        equipmentTypeName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListYan();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.equipmentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */

      getEquipemntTypeListYan() {
        getEquipemntTypeYan().then(response => {
          this.equipemntType = response.data
        })
      },

    handleAdd() {
      this.reset();
      this.openAdd = true;
      this.title = "添加设备库存表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const equipmentId = row.equipmentId || this.ids
      getEquipemnt(equipmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备库存表";
      });
      listEquipment_type(this.queryParams).then(response => {
        this.equipment_typeList = response.rows;
      });
    },
    /** 库存修改按钮操作 */
    handleUpdateYanRu(row) {
      this.reset();
      const equipmentId = row.equipmentId || this.ids
      getEquipemntByIdYan(equipmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "入库设备";
      });

    },

    handleUpdateYanChu(row) {
      this.reset();
      const equipmentId = row.equipmentId || this.ids
      getEquipemntByIdYan(equipmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "出库设备";
      });
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.equipmentId != null) {

            if(this.title == "入库设备"){
              this.form.equipmentNum =  Number(this.form.equipmentNum) + Number(this.form.equipmentNums);
            } else{
              if(  Number(this.form.equipmentNum)<Number(this.form.equipmentNums)){
                alert("出库量过大")
              }else{
                this.form.equipmentNum = Number(this.form.equipmentNum) - Number(this.form.equipmentNums);
              }
            }
            updateEquipemntYan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getListYan();
            });
          } else {
            addEquipemntYan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const equipmentIds = row.equipmentId || this.ids;
      this.$modal.confirm('是否确认删除设备库存表编号为"' + equipmentIds + '"的数据项？').then(function() {
        return delEquipemnt(equipmentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hospital/equipemnt/export', {
        ...this.queryParams
      }, `equipemnt_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
